package com.ruoyi.common.core.model;

import cn.hutool.extra.spring.SpringUtil;
import com.eic.framework.core.security.SecuritySession;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.model.JwtLoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.framework.service.SysPermissionService;
import com.ruoyi.system.domain.dao.SysUser;
import com.ruoyi.system.service.ISysUserService;

import java.util.Set;

public class AdminLoginUser extends JwtLoginUser
{
    private static RedisCache cache;
    private static ISysUserService userService;
    private static SysPermissionService permissionService;

    @Override
    public Set<String> getPermissions() {
        return getUser().getPermissions();
    }

    private AdminUser getUser(){
        AdminUser user= (AdminUser)SecuritySession.getSession(CacheConstants.ADMIN_SESSION_USER);
        if(user==null){
            user=new AdminUser();
            SysUser sysUser =getUserService().getById(getUserId());
            user.setUserName(sysUser.getUserName());
            user.setPermissions(getPermissionService().getMenuPermission(getUserId()));
            SecuritySession.setSession(CacheConstants.ADMIN_SESSION_USER,user);
        }
        return user;
    }

    private ISysUserService getUserService(){
        if(userService==null){
            userService=SpringUtil.getBean(ISysUserService.class);
        }
        return userService;
    }
    private SysPermissionService getPermissionService(){
        if(permissionService==null){
            permissionService=SpringUtil.getBean(SysPermissionService.class);
        }
        return permissionService;
    }

}
